package com.message.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.flink.api.java.utils.ParameterTool;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
@Slf4j
public class ConfigProperties {
    private static Properties properties =null;
    static {
        if (properties == null) {
            synchronized (ConfigProperties.class) {
                if (properties == null) {//双重校验锁，防止多线程环境下出现初始化多个线程池的情况
                    InputStream resourceAsStream = ConfigProperties.class.getClassLoader().getResourceAsStream("config.properties");
                    properties = new Properties();
                    try {
                        properties.load(resourceAsStream);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private ConfigProperties() {
    }

    public static String getPropertyValue(String key) {
        return properties.getProperty(key);
    }

    public static Long getPropertyLongValue(String key) {
        return Long.parseLong(properties.getProperty(key));
    }

    public static void setPropertyValue(String key,String value) {
        properties.setProperty(key,value);
    }

    public static void loadArgs(String[] args) {
        ParameterTool tool = ParameterTool.fromArgs(args);
        ConfigProperties.setPropertyValue("url",tool.get("url","jdbc:postgresql://172.16.240.167:5432/openproject?TimeZone=UTC"));
        ConfigProperties.setPropertyValue("username",tool.get("username","postgres"));
        ConfigProperties.setPropertyValue("password",tool.get("password","p4ssw0rd"));
        ConfigProperties.setPropertyValue("redis.ip",tool.get("redis.ip","172.16.100.243"));
        ConfigProperties.setPropertyValue("redis.port",tool.get("redis.port","6379"));
        String redispass = tool.get("redis.password", "");
        if(!"".equals(redispass)) {
            ConfigProperties.setPropertyValue("redis.password", redispass);//如果redis密码配置不为空
        }
        ConfigProperties.setPropertyValue("dingding.appid",tool.get("dingding.appid","dingagld9cdt0bbvk1h3"));
        ConfigProperties.setPropertyValue("dingding.appkey",tool.get("dingding.appkey","QWgmxKt-2imeOSAoHjbYX9z1ym-fDyAiXpi22WlAre9hITssF_PYOKUxMeEZvaGH"));
        ConfigProperties.setPropertyValue("dingding.agentId",tool.get("dingding.agentId","1679423680"));
        System.out.println("配置"+properties.values().toString());
    }

    public static Properties getProperties() {
        return properties;
    }

}
